import tkinter as tk
import pyautogui
import sys

def main():
    # 创建主窗口
    root = tk.Tk()
    root.title("坐标标记器")
    root.attributes('-topmost', True)  # 窗口置顶
    
    # 获取屏幕尺寸
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    
    # 设置窗口大小和位置
    root.geometry(f"{screen_width}x{screen_height}+0+0")
    root.attributes('-alpha', 0.3)  # 设置透明度
    root.attributes('-fullscreen', True)  # 全屏显示
    
    # 判断是否提供了坐标参数
    if len(sys.argv) == 3:
        try:
            target_x = int(sys.argv[1])
            target_y = int(sys.argv[2])
            
            # 创建画布
            canvas = tk.Canvas(root, width=screen_width, height=screen_height)
            canvas.pack()
            
            # 绘制目标坐标点
            marker_size = 10
            canvas.create_oval(
                target_x - marker_size, target_y - marker_size,
                target_x + marker_size, target_y + marker_size,
                fill='red'
            )
            
            # 添加十字线
            canvas.create_line(target_x, 0, target_x, screen_height, fill='red', width=1)
            canvas.create_line(0, target_y, screen_width, target_y, fill='red', width=1)
            
            # 添加坐标文本
            canvas.create_text(
                target_x + 20, target_y - 20,
                text=f"({target_x}, {target_y})",
                fill='red',
                font=('Arial', 16, 'bold')
            )
            
            # 添加相对位置信息
            distance_right = screen_width - target_x
            distance_bottom = screen_height - target_y
            
            info_text = f"""
坐标: ({target_x}, {target_y})
距左边: {target_x}像素
距右边: {distance_right}像素
距上边: {target_y}像素
距下边: {distance_bottom}像素
屏幕尺寸: {screen_width}x{screen_height}
位置: 屏幕{get_position_description(target_x, target_y, screen_width, screen_height)}
"""
            canvas.create_text(
                screen_width//2, 50,
                text=info_text,
                fill='blue',
                font=('Arial', 14, 'bold')
            )
            
            # 按任意键退出
            root.bind("<Key>", lambda e: root.destroy())
            root.bind("<Button>", lambda e: root.destroy())
            
        except ValueError:
            print("坐标必须是整数")
            return
    else:
        # 如果没有提供坐标，显示鼠标当前位置
        label = tk.Label(root, text="移动鼠标查看坐标", font=('Arial', 14))
        label.pack(pady=10)
        
        def update_mouse_position():
            x, y = pyautogui.position()
            label.config(text=f"当前鼠标坐标: ({x}, {y})")
            root.after(100, update_mouse_position)
        
        update_mouse_position()
        
        # 按ESC键退出
        def on_escape(event):
            if event.keysym == 'Escape':
                root.destroy()
                
        root.bind("<Key>", on_escape)

    root.mainloop()

def get_position_description(x, y, width, height):
    """获取点在屏幕上的位置描述"""
    horizontal = ""
    vertical = ""
    
    # 水平位置
    if x < width * 0.33:
        horizontal = "左"
    elif x < width * 0.66:
        horizontal = "中"
    else:
        horizontal = "右"
        
    # 垂直位置
    if y < height * 0.33:
        vertical = "上"
    elif y < height * 0.66:
        vertical = "中"
    else:
        vertical = "下"
        
    return f"{vertical}{horizontal}方"

if __name__ == "__main__":
    main()